package cn.fl.project.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.*;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.logger.eum.LogOperEnum;
import cn.fl.capital.condition.CapPayInfoQueryCondition;
import cn.fl.capital.constant.CapPayInfoConstant;
import cn.fl.capital.dto.CapPayInfoDTO;
import cn.fl.capital.service.ICapPayInfoApiService;
import cn.fl.capital.transform.CapPayInfoVOTransform;
import cn.fl.capital.vo.CapPayInfoVO;
import cn.fl.expatriate.api.IExpBizManRecApiService;
import cn.fl.expatriate.condition.ExpBizManRecQC;
import cn.fl.expatriate.dto.ExpBizManRecDTO;
import cn.fl.framework.base.constant.WebBaseConstant;
import cn.fl.framework.constant.DictionaryConstant;
import cn.fl.project.api.*;
import cn.fl.project.constant.PrjBscInfoConstant;
import cn.fl.project.constant.PrjPrdDtlConstant;
import cn.fl.project.dto.PrjPrdBscInfoDTO;
import cn.fl.project.dto.PrjPrdExtDTO;
import cn.fl.project.service.IPrjPrdBscInfoService;
import cn.fl.project.service.IVehicleStyleService;
import cn.fl.project.transform.*;
import cn.fl.project.vo.*;
import cn.fl.system.constant.SysLogConstant;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import fl.flow.facade.flow.api.IFlowQueryApiService;
import fl.flow.facade.flow.condition.SysFlowSHQueryCondition;
import fl.flow.facade.flow.dto.SysFlowSHDTO;
import fl.spl.facade.spl.api.ISplCommPropInfoApiService;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.product.transform.PrdBscInfoVOTransform;
import cn.fl.project.condition.PrjPrdDtlQueryCondition;
import cn.fl.project.dto.PrjPrdDtlDTO;
import cn.fl.project.service.IPrjPrdDtlService;
import cn.fl.project.service.IProjectApiService;
import fl.product.facade.product.api.IProductApiService;
import fl.product.facade.product.condition.PrdBscInfoQueryCondition;
import fl.product.facade.product.dto.PrdBscInfoDTO;
import ins.car.facade.car.prj.api.InsOrPrjCombinationApiService;
/**
 * @Description: 项目产品明细信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class PrjPrdDtlServiceImpl implements IPrjPrdDtlService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(IPrjPrdDtlService.class);
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IProjectApiService projectApiService;
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IProjectModifyApiService projectModifyApiService;
	
	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IProductApiService productApiService;

	@Autowired
	private IProjectQueryApiService projectQueryApiService;

	@Autowired
	private InsOrPrjCombinationApiService insOrPrjCombinationApiService;

    @Autowired
    ISplCommPropInfoApiService splCommPropInfoApiService;

	@Autowired
	private IExpBizManRecApiService expBizManRecApiService;

	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	/**
	 * API 暴露服务
	 */
	@Autowired
	private ICapPayInfoApiService capPayInfoApiService;

	@Autowired
	private IFlowQueryApiService flowQueryApiService;

	@Override
	public PrjPrdDtlVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//TODO 业务逻辑
		PrjPrdDtlDTO dto = this.projectApiService.selectPrjPrdDtlById(id);
		return PrjPrdDtlVOTransform.toVO(dto);
	}

	@Override
	public Long add(PrjPrdDtlVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.prjPrdDtlApiService.add(PrjPrdDtlVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.prjPrdDtlApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return projectApiService.removePrjPrdDtlByIds(ids);
    }

	@Override
	public boolean modify(PrjPrdDtlVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.projectModifyApiService.modifyInfo(PrjPrdDtlVOTransform.toDTO(entity));
	}

	@Override
	public boolean modifyDtlForOpt(PrjPrdDtlVO entity) throws BaseException {
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return this.projectModifyApiService.modifyDtlForOpt(PrjPrdDtlVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		page = this.projectApiService.searchPrjPrdDtlListPage(page,CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
		List<PrjPrdDtlVO> list = PrjPrdDtlVOTransform.toVOList((List<PrjPrdDtlDTO>)page.getResultList());

		if(CollectionUtils.isNotEmpty(list)){
		/*	List<Long> prdIdList = new ArrayList<Long>();
			for (PrjPrdDtlVO prjPrdDtlVO : list) {
				if(null!=prjPrdDtlVO.getPrdId()){
					prdIdList.add(prjPrdDtlVO.getPrdId());
				}
			}
			if(CollectionUtils.isNotEmpty(prdIdList)){
				PrdBscInfoQueryCondition prdQc = new PrdBscInfoQueryCondition();
				prdQc.setIds(prdIdList);
				List<PrdBscInfoDTO> prdList = this.productApiService.searchPrdBscInfoList(prdQc, CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd());
				if(CollectionUtils.isNotEmpty(prdList)){
					for (PrjPrdDtlVO prjPrdDtlVO : list) {
						for (PrdBscInfoDTO prdBscInfoDTO : prdList) {
							if(prjPrdDtlVO.getPrdId().equals(prdBscInfoDTO.getId())){
								prjPrdDtlVO.setPrdBscInfoVO(PrdBscInfoVOTransform.toVO(prdBscInfoDTO));
							}
						}
						prdIdList.add(prjPrdDtlVO.getPrdId());
					}
				}
			}
			*/
			
			page.setResultList(list);
		}
		
		return page;
	}

	@Override
	public void modifySplitPrjPrdAndPaySch(List<Long> prjPrdIdList, Long prjId) throws BaseException {
		if(CollectionUtils.isEmpty(prjPrdIdList)||StringUtils.isEmpty(prjId)){
			BaseLogUtils.info(logger, "splitPrjPrdAndPaySch", "项目ID和项目商品ID列表参数不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		this.projectApiService.modifySplitPrjPrdAndPaySch(prjPrdIdList,prjId,CurrentThreadContext.getCurrentUserId(),CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentBuOrgCd());
	}

	@Override
	public List<PrjPrdDtlVO> selectList(PrjPrdDtlQueryCondition prjPrdDtlQc) {
		if(null==prjPrdDtlQc){
				BaseLogUtils.info(logger, "selectList", "项目产品ID参数不能为空!", CurrentThreadContext.getCurrentUserName());
				throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		prjPrdDtlQc.setSysCd(CurrentThreadContext.getCurrentSysCd());
		prjPrdDtlQc.setOrgCd(CurrentThreadContext.getCurrentOrgCd());
		return PrjPrdDtlVOTransform.toVOList(projectApiService.selectPrjPrdDtlList(prjPrdDtlQc));
	}

	@Override
	public void modifyPrdDtlLnkComp(List<Long> idList, Long lnkCmpId,String lnkCompNm, String lnkCmpMsg, String lnkCmpType) {
		if(CollectionUtils.isEmpty(idList)||StringUtils.isEmpty(lnkCmpId)){
			BaseLogUtils.info(logger, "modifyPrdDtlLnkComp", "挂靠公司ID参数不能为空,产品明细ID不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		
		this.projectApiService.modifyPrdDtlLnkComp(idList,lnkCmpId,lnkCompNm,lnkCmpMsg,lnkCmpType);
		
	}

	@Override
	public void modifyPrdDtlLnkCompNew(List<Long> idList, Long lnkCmpId,String lnkCompNm,Long lnkStartLvl, String lnkCmpMsg, String lnkCmpType) {
		if(CollectionUtils.isEmpty(idList)||StringUtils.isEmpty(lnkCmpId)){
			BaseLogUtils.info(logger, "modifyPrdDtlLnkCompNew", "挂靠公司ID参数不能为空,产品明细ID不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		this.projectApiService.modifyPrdDtlLnkCompNew(idList,lnkCmpId,lnkCompNm,lnkStartLvl,lnkCmpMsg,lnkCmpType);

	}

	@Override
	public void removePrdDtlLnkCompByIds(List<Long> idList) throws BaseException {
		if(CollectionUtils.isEmpty(idList)){
			BaseLogUtils.info(logger, "removePrdDtlLnkCompByIds", "产品明细ID列表参数不能为空!", CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600,BaseExceptionConstant.BASE_EXCEPTION_CODE_600_NAME);
		}
		this.projectApiService.removePrdDtlLnkCompByIds(idList);
	}

	@Override
	public void modifyPrdDtlPrdInfo(PrjPrdDtlVO dtl,PrjPrdDtlTkVO tk,PrjPrdDtlCarVO car) throws BaseException {
		this.projectApiService.modifyPrdDtlPrdInfo(PrjPrdDtlVOTransform.toDTO(dtl),PrjPrdDtlTkVOTransform.toDTO(tk),PrjPrdDtlCarVOTransform.toDTO(car));
	}

	@Override
	public boolean modifyPrjPrdDtlList(List<PrjPrdDtlVO> list) throws BaseException {
		if(CollectionUtils.isEmpty(list)){
			return false;
		}
		
		return this.projectApiService.modifyPrjPrdDtlList(PrjPrdDtlVOTransform.toDTOList(list));
	}

	@Override
	public Pager searchPrjPayListPage(Pager page) throws BaseException {
		
		Pager pager = 	projectQueryApiService.searchPrjPayListPage(page);
		List<PrjPrdDtlDTO> dtoList =  (List<PrjPrdDtlDTO>)pager.getResultList();
		if(CollectionUtils.isNotEmpty(dtoList)){
			pager.setResultList(PrjPrdDtlVOTransform.toVOList(dtoList));
		}
 		return pager;
	}

	@Override
	public boolean modifyPrjPrdDtlLnkCompGps(PrjPrdDtlVO entity) throws BaseException {
		if(null==entity){ return false;}
		return projectModifyApiService.modifyPrjPrdDtlLnkCompGps(PrjPrdDtlVOTransform.toDTO(entity));
	}

	public boolean checkPrjLnkCompCrAmtValue(Long lnkCmpId) throws BaseException {
		
		return projectModifyApiService.checkPrjLnkCompCrAmtValue(lnkCmpId);
	}
	@Autowired
	private IVehicleStyleService styleService;
    @Autowired
    private IPrjPrdDtlExtApiService prjPrdDtlExtApiService;
    @Autowired
    private IPrjPrdBscInfoService prjPrdBscInfoService;

    @Autowired
	private IPrjPrdExtApiService prjPrdExtApiService;
    // 换车日志
    public static final Logger CHANGE_LOG=LoggerFactory.getLogger("changeCarLogger");
	@Override
	public void modifyPrd(Long dtlId, Long styleId, String carLen, String prdLvlCd, String auth,String prdCd) {
		// 查询车型库参配信息
		PrjPrdParamConfigVO prdDetail = styleService.getPrdDetail(styleId);

        CHANGE_LOG.info("用户id:{},姓名：{}开始进行换车操作。",CurrentThreadContext.getCurrentUserId(),CurrentThreadContext.getCurrentUserName());
		// 修改主表信息start
		PrjPrdDtlVO dtlVO = this.selectById(dtlId);
		CHANGE_LOG.info("本次项目产品明细（prj_prd_dtl）修改前 JSON：{}",JSON.toJSONString(dtlVO));
		if(auth.equals("1")){
			if(dtlVO != null){
				Long prjPrdId = dtlVO.getPrjPrdId();
				PrjPrdBscInfoDTO prjPrdBscInfoDTO = this.projectApiService.selectPrjPrdBscInfoAndExtById(String.valueOf(prjPrdId),CurrentThreadContext.getCurrentSysCd(),CurrentThreadContext.getCurrentOrgCd());
				PrjPrdBscInfoVO prjPrdBscInfoVO = PrjPrdBscInfoVOTransform.toVO(prjPrdBscInfoDTO);//this.prjPrdBscInfoService.selectById(prjPrdId);
					// 如果是多台车   需要判断品牌车系是否一致 不一致的情况下 需要拆项目产品
					String brCd = prjPrdBscInfoVO.getBrCd();// 品牌
					String modelNm = prjPrdBscInfoVO.getModelNm(); //车系
					Integer prdQty = prjPrdBscInfoVO.getPrdQty();

					BaseLogUtils.info(logger,"modifyPrd","=运维换车.dtlId:"+dtlId+",styleId:"+styleId+",prdDetail:"+JSON.toJSONString(prdDetail)+",prjPrdBscInfoVO:"+JSON.toJSONString(prjPrdBscInfoVO),CurrentThreadContext.getCurrentUserName());
					if(StringUtils.isNotBlank(brCd) && StringUtils.isNotBlank(modelNm) && brCd.equals(prdDetail.getBrCd()) && modelNm.equals(prdDetail.getModelName())){
						// 一致
						// 就不需要换了
						CHANGE_LOG.info("用户选择的品牌车系与之前一致，因此项目产品本次不需要进行修改。");
					}else{// 不一致
						CHANGE_LOG.info("正在进行项目产品(prj_prd_bsc_info)修改 prj_prd_bsc_info.id:{}",prjPrdBscInfoVO.getId());
						if(prdQty >1 ) {
							CHANGE_LOG.info("本次换车为：多台车换车过程");
							// 数量-1  增加一个新产品
							CHANGE_LOG.info("产品数量-1 prj_prd_bsc_info.id:{}",prjPrdBscInfoVO.getId());
							prjPrdBscInfoVO.setPrdQty(prdQty - 1);
							prjPrdBscInfoVO.setPrdAmt( prjPrdBscInfoVO.getPrdAmt() - prjPrdBscInfoVO.getGuidePc());
							this.prjPrdBscInfoService.modify(prjPrdBscInfoVO);
							PrjPrdBscInfoVO vo = new PrjPrdBscInfoVO();
							copyPrjPrdBscInfoVO(styleId, prdDetail, prjPrdBscInfoVO, vo);
							vo.setPrdCd(prdCd);
							String purposePCd = this.styleService.getPurposePCd(Long.valueOf(prdCd));
							vo.setPrdTypCd(purposePCd);
							vo.setPrdDtlId(dtlId);
							CHANGE_LOG.info("新增项目产品基础信息(prj_prd_bsc_info) JSON:{}",JSON.toJSONString(vo));
							Long add = this.prjPrdBscInfoService.add(vo);
							//新增产品对应账号信息
							List<PrjSplBankAccRVO> prjSplBankAccRVOList= prjPrdBscInfoVO.getPrjSplBankAccRVOList();
							if(CollectionUtils.isNotEmpty(prjSplBankAccRVOList)){
								for (PrjSplBankAccRVO prjSplBankAccRVO : prjSplBankAccRVOList){
									prjSplBankAccRVO.setPrjPrdId(add);
									this.projectModifyApiService.addPrjSplBankAccR(PrjSplBankAccRVOTransform.toDTO(prjSplBankAccRVO));
								}
							}
							// 增加ext
							PrjPrdExtDTO ext = new PrjPrdExtDTO();
							setExtDto(prdDetail, ext);
							ext.setId(add);
							CHANGE_LOG.info("新增项目产品基础信息扩展表(prj_prd_ext) JSON:{}",JSON.toJSONString(ext));
							this.prjPrdExtApiService.add(ext);
							CHANGE_LOG.info("修改项目产品明细 set prj_prd_dtl.prj_prd_dtl 原始:{},新值{}",dtlVO.getPrjPrdId(),add);
							dtlVO.setPrjPrdId(add);
							this.modify(dtlVO);
						}else{
							CHANGE_LOG.info("本次换车为：单台车换车过程");
							//单台车
							// 修改项目产品
							CHANGE_LOG.info("项目产品信息(prj_prd_bsc_info) 修改前 JSON：{}",JSON.toJSONString(prjPrdBscInfoVO));
							prjPrdBscInfoVO.setStyleId(styleId);
							prjPrdBscInfoVO.setCarStyleName(prdDetail.getCarStyleName());
							prjPrdBscInfoVO.setModelNm(prdDetail.getModelName());
							prjPrdBscInfoVO.setBrCd(prdDetail.getBrCdNm());
							prjPrdBscInfoVO.setPrdCd(prdCd);
							String purposePCd = this.styleService.getPurposePCd(Long.valueOf(prdCd));
							prjPrdBscInfoVO.setPrdTypCd(purposePCd);
							prjPrdBscInfoVO.setPrdNm(prdDetail.getPrdNm());
							prjPrdBscInfoVO.setPrdMdl(prdDetail.getPrdMdl());
							this.prjPrdBscInfoService.modify(prjPrdBscInfoVO);
							CHANGE_LOG.info("项目产品信息(prj_prd_bsc_info) 修改后 JSON：{}",JSON.toJSONString(prjPrdBscInfoVO));
							// 修改项目产品EXT
							PrjPrdExtDTO extDTO = this.prjPrdExtApiService.selectById(prjPrdId);
							CHANGE_LOG.info("项目产品信息(prj_prd_ext) 修改前 JSON：{}",JSON.toJSONString(extDTO));
							setExtDto(prdDetail, extDTO);
							this.prjPrdExtApiService.modify(extDTO);
							CHANGE_LOG.info("项目产品信息(prj_prd_ext) 修改后 JSON：{}",JSON.toJSONString(extDTO));

						}
					}
			}
		}

		// 修改主表信息END
		String engMdl = prdDetail.getEngMdl();
		// 更新的话 至少俩数据得有一个吧，不然更新啥
		if(dtlId !=null && ( StringUtils.isNotBlank(prdDetail.getPrdMdl()) || StringUtils.isNotBlank(prdDetail.getEngMdl()) ) ){
			PrjPrdDtlVO prjPrdDtlVO = new PrjPrdDtlVO();
			prjPrdDtlVO.setId(dtlId);
			prjPrdDtlVO.setPrdMdl(prdDetail.getPrdMdl());
			prjPrdDtlVO.setEngMdl(engMdl);
			if("1".equals(auth)){
				prjPrdDtlVO.setPrdNm(prdDetail.getPrdNm());
			}
			CHANGE_LOG.info("本次项目产品明细（prj_prd_dtl）的修改内容 JSON：{}",JSON.toJSONString(prjPrdDtlVO));
			this.modify(prjPrdDtlVO);
		}else{
			CHANGE_LOG.info("明细主表更新失败，原因：车型库(参配信息)中没有公告型号（产品型号）或发动机号");
			throw new BaseException("明细主表更新失败，原因：车型库(参配信息)中没有公告型号（产品型号）或发动机号");
		}
		// 拷贝属性
		PrjPrdDtlExtVO2 vo = new PrjPrdDtlExtVO2();
		if(StringUtils.isNotBlank(carLen)){
			vo.setCarLen(Long.valueOf(carLen));
		}
		if(StringUtils.isNotBlank(prdLvlCd)){
			vo.setPrdLvlCd(prdLvlCd);
		}

        try {
            BeanUtils.copyProperties(vo,prdDetail);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        if(StringUtils.isNotBlank(prdDetail.getDrvTypCd())){
			vo.setDrvTyp(prdDetail.getDrvTypCd());
		}else {
		    vo.setDrvTyp("");
        }
		if(StringUtils.isNotBlank(engMdl)){
			vo.setEngMdl(engMdl);
		}else{
		    vo.setEngMdl("");
        }
		if(StringUtils.isNotBlank(prdDetail.getHp())){
			vo.setHp(Long.parseLong(prdDetail.getHp()));
		}else{
		    vo.setHp(0l);
        }
        if(StringUtils.isNotBlank(prdDetail.getEmiStdCd())){
            vo.setEmiStd(prdDetail.getEmiStdCd());
            vo.setEmiStdCd(prdDetail.getEmiStdCd());
        }else{
		    vo.setEmiStd("");
		    vo.setEmiStdCd("");
        }
        if(StringUtils.isNotBlank(prdDetail.getCabSpec())){
            vo.setCabSpec(prdDetail.getCabSpec());
        }else{
		    vo.setCabSpec("");
        }
        if(StringUtils.isNotBlank(prdDetail.getGrboxMdl())){
            vo.setGrboxMdl(prdDetail.getGrboxMdl());
        }else{
			vo.setGrboxMdl("");
		}
        if(StringUtils.isNotBlank(prdDetail.getRearAxleSpec())){
            vo.setRearAxleSpec(prdDetail.getRearAxleSpec());
        }else{
			vo.setRearAxleSpec("");
		}
        if(StringUtils.isNotBlank(prdDetail.getmWhlSpec())){
            vo.setMWhlSpec(prdDetail.getmWhlSpec());
        }else{
			vo.setMWhlSpec("");
		}
        if(StringUtils.isNotBlank(prdDetail.getWhlBase())){
            vo.setWhlBase(prdDetail.getmWhlSpec());
        }else{
			vo.setWhlBase("");
		}
        if(StringUtils.isNotBlank(prdDetail.getCntrSz())){
            vo.setCntrSz(prdDetail.getCntrSz());
        }else{
			vo.setCntrSz("");
		}
        if(StringUtils.isNotBlank(prdDetail.getActCntrSz())){
            vo.setActCntrSz(prdDetail.getActCntrSz());
        }else{
			vo.setActCntrSz("");
		}
        if(StringUtils.isNotBlank(prdDetail.getMaxHp())){
            vo.setMaxHp(prdDetail.getMaxHp());
        }else{
			vo.setMaxHp("");
		}
        if(StringUtils.isNotBlank(prdDetail.getCarStrcCd())){
            vo.setCarStrcCfg(prdDetail.getCarStrcCd());
        }else{
			vo.setCarStrcCfg("");
		}
        if(StringUtils.isNotBlank(prdDetail.getDoorInfo())){
            vo.setDoorNum(prdDetail.getDoorInfo());
        }else{
			vo.setDoorNum("");
		}
        if(StringUtils.isNotBlank(prdDetail.getMaxSpd())){
            vo.setMaxSpd(prdDetail.getMaxSpd());
        }else{
			vo.setMaxSpd("");
		}
		vo.setId(dtlId);
		PrjPrdExtDTO before = this.prjPrdExtApiService.selectById(dtlId);
		CHANGE_LOG.info("项目产品明细附加表（prj_prd_dtl_ext）修改前 JSON：{}",JSON.toJSONString(before));
		// 更新dtlext
		prjPrdDtlExtApiService.modify(PrjPrdDtlExtVOTransform.toDTO(vo));
		CHANGE_LOG.info("项目产品明细附加表（prj_prd_dtl_ext）修改后 JSON：{}",JSON.toJSONString(vo));
	}

	public void setExtDto(PrjPrdParamConfigVO prdDetail, PrjPrdExtDTO extDTO) {
		String drvTypCd = prdDetail.getDrvTypCd();
		if(StringUtils.isNotBlank(drvTypCd)){
			extDTO.setDrvTyp(drvTypCd);
			extDTO.setDrvTypCd(drvTypCd);
		}else{
			extDTO.setDrvTyp("");
			extDTO.setDrvTypCd("");
		}
		String engMdl = prdDetail.getEngMdl();
		if(StringUtils.isNotBlank(engMdl)){
			extDTO.setEngMdl(engMdl);
		}else{
			extDTO.setEngMdl("");
		}
		String hp = prdDetail.getHp();
		if(StringUtils.isNotBlank(hp)){
			extDTO.setHp(Long.valueOf(hp));
		}else{
			extDTO.setHp(0l);
		}
		String emiStd = prdDetail.getEmiStd();
		if(StringUtils.isNotBlank(emiStd)){
			extDTO.setEmiStd(emiStd);
			extDTO.setEmiStdCd(emiStd);
		}else{
			extDTO.setEmiStd("");
			extDTO.setEmiStdCd("");
		}
		String cabSpec = prdDetail.getCabSpec();
		if(StringUtils.isNotBlank(cabSpec)){
			extDTO.setCabSpec(cabSpec);
		}else{
			extDTO.setCabSpec("");
		}
		String grboxMdl = prdDetail.getGrboxMdl();
		if(StringUtils.isNotBlank(grboxMdl)){
			extDTO.setGrboxMdl(grboxMdl);
		}else{
			extDTO.setGrboxMdl("");
		}
		String rearAxleSpec = prdDetail.getRearAxleSpec();
		if(StringUtils.isNotBlank(rearAxleSpec)){
			extDTO.setRearAxleSpec(rearAxleSpec);
		}else{
			extDTO.setRearAxleSpec("");
		}
		String mwhlSpec = prdDetail.getmWhlSpec();
		if(StringUtils.isNotBlank(mwhlSpec)){
			extDTO.setmWhlSpec(mwhlSpec);
		}else{
			extDTO.setmWhlSpec("");
		}
		String whlBase = prdDetail.getWhlBase();
		if(StringUtils.isNotBlank(whlBase)){
			extDTO.setWhlBase(whlBase);
		}else{
			extDTO.setWhlBase("");
		}
		String cntrSz = prdDetail.getCntrSz();
		if(StringUtils.isNotBlank(cntrSz)){
			extDTO.setCntrSz(cntrSz);
		}else{
			extDTO.setCntrSz("");
		}

		String actCntrSz = prdDetail.getActCntrSz();
		if(StringUtils.isNotBlank(actCntrSz)){
			extDTO.setActCntrSz(actCntrSz);
		}else{
			extDTO.setCntrSz("");
		}
		String thickness = prdDetail.getThickness();
		if(StringUtils.isNotBlank(thickness)){
			extDTO.setThickness(thickness);
		}else{
			extDTO.setThickness("");
		}
		String axleInfo = prdDetail.getAxleInfo();
		if(StringUtils.isNotBlank(axleInfo)){
			extDTO.setAxleInfo(axleInfo);
		}else{
			extDTO.setAxleInfo("");
		}
		String matQalCd = prdDetail.getMatQalCd();
		if(StringUtils.isNotBlank(matQalCd)){
			extDTO.setMatQalCd(matQalCd);
		}else{
			extDTO.setMatQalCd("");
		}
		String liftWayCd = prdDetail.getLiftWayCd();
		if(StringUtils.isNotBlank(liftWayCd)){
			extDTO.setLiftWayCd(liftWayCd);
		}else{
			extDTO.setLiftWayCd("");
		}
		String doorInfo = prdDetail.getDoorInfo();
		if(StringUtils.isNotBlank(doorInfo)){
			extDTO.setDoorInfo(doorInfo);
		}else{
			extDTO.setDoorInfo("");
		}
		Integer isHvWhl = prdDetail.getIsHvWhl();
		if(isHvWhl != null){
			extDTO.setIsHvWhl(isHvWhl);
		}else{
			extDTO.setIsHvWhl(0);
		}
		String trlWhlSpec = prdDetail.getTrlWhlSpec();
		if(StringUtils.isNotBlank(trlWhlSpec)){
			extDTO.setTrlWhlSpec(trlWhlSpec);
		}else{
			extDTO.setTrlWhlSpec("");
		}
		String cylSepc = prdDetail.getCylSepc();
		if(StringUtils.isNotBlank(cylSepc)){
			extDTO.setCylSepc(cylSepc);
		}else{
			extDTO.setCylSepc("");
		}
		String tankVol = prdDetail.getTankVol();
		if(StringUtils.isNotBlank(tankVol)){
			extDTO.setTankVol(tankVol);
		}else{
			extDTO.setTankVol("");
		}
		String othInfo = prdDetail.getOthInfo();
		if(StringUtils.isNotBlank(othInfo)){
			extDTO.setOthInfo(othInfo);
		}else{
			extDTO.setOthInfo("");
		}
		String prdClsCd = prdDetail.getPrdClsCd();
		if(StringUtils.isNotBlank(prdClsCd)){
			extDTO.setPrdClsCd(prdClsCd);
		}else{
			extDTO.setPrdClsCd("");
		}
		extDTO.setMdfTm(new Date());
		extDTO.setMdfUsrId(CurrentThreadContext.getCurrentUserId());
		String fuelTypCd = prdDetail.getFuelTypCd();
		if(StringUtils.isNotBlank(fuelTypCd)){
			extDTO.setFuelTypCd(fuelTypCd);
		}else{
			extDTO.setFuelTypCd("");
		}
		String carLen = prdDetail.getCarLen();
		if(StringUtils.isNotBlank(carLen)){
			extDTO.setCarLen(carLen);
		}else{
			extDTO.setCarLen("");
		}
		String outVol = prdDetail.getOutVol();
		if(StringUtils.isNotBlank(outVol)){
			extDTO.setOutVol(outVol);
		}else{
			extDTO.setOutVol("");
		}

		String grboxTypCd = prdDetail.getGrboxTypCd();
		if(StringUtils.isNotBlank(grboxTypCd)){
			extDTO.setGrboxTypCd(grboxTypCd);
		}else{
			extDTO.setGrboxTypCd("");
		}
		String grboxCfg = prdDetail.getGrboxCfg();
		if(StringUtils.isNotBlank(grboxCfg)){
			extDTO.setGrboxCfg(grboxCfg);
		}else{
			extDTO.setGrboxCfg("");
		}
		String seatQtyCd = prdDetail.getSeatQtyCd();
		if(StringUtils.isNotBlank(seatQtyCd)){
			extDTO.setSeatQtyCd(seatQtyCd);
		}else{
			extDTO.setSeatQtyCd("");
		}
		String carStrcCd = prdDetail.getCarStrcCd();
		if(StringUtils.isNotBlank(carStrcCd)){
			extDTO.setCarStrcCd(carStrcCd);
		}else{
			extDTO.setCarStrcCd("");
		}
		String carStrcCfg = prdDetail.getCarStrcCfg();
		if(StringUtils.isNotBlank(carStrcCfg)){
			extDTO.setCarStrcCfg(carStrcCfg);
		}else{
			extDTO.setCarStrcCfg("");
		}
		String powerTypCd = prdDetail.getPowerTypCd();
		if(StringUtils.isNotBlank(powerTypCd)){
			extDTO.setPowerTypCd(powerTypCd);
		}else{
			extDTO.setPowerTypCd("");
		}
		String maxSpd = prdDetail.getMaxSpd();
		if(StringUtils.isNotBlank(maxSpd)){
			extDTO.setMaxSpd(maxSpd);
		}else{
			extDTO.setMaxSpd("");
		}
		String carMdl = prdDetail.getCarMdl();
		if(StringUtils.isNotBlank(carMdl)){
			extDTO.setCarMdl(carMdl);
		}else{
			extDTO.setCarMdl("");
		}
		String maxHp = prdDetail.getMaxHp();
		if(StringUtils.isNotBlank(maxHp)){
			extDTO.setMaxHp(maxHp);
		}else{
			extDTO.setMaxHp("");
		}
		String modelName = prdDetail.getModelName();
		if(StringUtils.isNotBlank(modelName)){
			extDTO.setModelName(modelName);
		}else{
			extDTO.setModelName("");
		}
		String productionCompany = prdDetail.getProductionCompany();
		if(StringUtils.isNotBlank(productionCompany)){
			extDTO.setProductionCompany(productionCompany);
		}else{
			extDTO.setProductionCompany("");
		}
	}

	public void copyPrjPrdBscInfoVO(Long styleId, PrjPrdParamConfigVO prdDetail, PrjPrdBscInfoVO prjPrdBscInfoVO, PrjPrdBscInfoVO vo) {
		vo.setPrjId(prjPrdBscInfoVO.getPrjId());
//						vo.setPrdId();
		vo.setRsrvPc(prjPrdBscInfoVO.getRsrvPc());
		vo.setPrdQty(1);
		vo.setPrdAmt(prjPrdBscInfoVO.getGuidePc());
		vo.setSplId(prjPrdBscInfoVO.getSplId());
		vo.setSplNm(prjPrdBscInfoVO.getSplNm());
		vo.setMfrId(prjPrdBscInfoVO.getMfrId());
		vo.setMfrNm(prjPrdBscInfoVO.getMfrNm());
		vo.setIrrPct(prjPrdBscInfoVO.getIrrPct());
		vo.setCertDt(prjPrdBscInfoVO.getCertDt());
		vo.setInvAmt(prjPrdBscInfoVO.getInvAmt());
		vo.setCrtTm(new Date());
		vo.setCrtUsrId(CurrentThreadContext.getCurrentUserId());
		vo.setPrdRem("运维换车创建");
		vo.setDeprRate(prjPrdBscInfoVO.getDeprRate());
		vo.setDeprAmt(prjPrdBscInfoVO.getDeprAmt());
		vo.setSplAccNoId(prjPrdBscInfoVO.getSplAccNoId());
		vo.setEvaLvl(prjPrdBscInfoVO.getEvaLvl());
		vo.setSplMfrId(prjPrdBscInfoVO.getSplMfrId());
		vo.setSplMfrNm(prjPrdBscInfoVO.getSplMfrNm());
		vo.setLicenseCd(prjPrdBscInfoVO.getLicenseCd());
		vo.setStyleId(styleId);
		vo.setModelNm(prdDetail.getModelName());
		vo.setCarStyleName(prdDetail.getCarStyleName());
		vo.setPrdNm(prdDetail.getPrdNm());
		vo.setPrdMdl(prdDetail.getPrdMdl());
		vo.setBrCd(prdDetail.getBrCdNm());
		vo.setIsNew(prjPrdBscInfoVO.getIsNew());
		vo.setUsedPd(prjPrdBscInfoVO.getUsedPd());
		vo.setGuidePc(prjPrdBscInfoVO.getGuidePc());
		vo.setReceiveTypeCd(prjPrdBscInfoVO.getReceiveTypeCd());
		vo.setSplStarLevel(prjPrdBscInfoVO.getSplStarLevel());
		vo.setPayWayCd(prjPrdBscInfoVO.getPayWayCd());
		vo.setVehCheckCode(prjPrdBscInfoVO.getVehCheckCode());
		vo.setReleaseDate(prjPrdBscInfoVO.getReleaseDate());
		vo.setLoanableAmt(prjPrdBscInfoVO.getLoanableAmt());
		vo.setIsDel(0);
	}

	@Override
	public PrjPrdDtlVO selectOneByCondition(PrjPrdDtlQueryCondition condition) {
		List<PrjPrdDtlVO> prjPrdDtlVOS = this.selectList(condition);
		if(CollectionUtils.isNotEmpty(prjPrdDtlVOS)){
			return prjPrdDtlVOS.get(0);
		}
		return null;
	}
	@Override
	public Long[] downPaymentAmount(Long prjPrdDtlId, Long paySchId) throws BaseException {
		Long[] amountPayable = new Long[2];
		Long firstAmt = 0L;
		Long totalAmt = 0L;
		if (prjPrdDtlId != null && paySchId != null ) {
			java.math.BigDecimal[] payAmt = this.insOrPrjCombinationApiService.amountOfPrice(paySchId, prjPrdDtlId);
			for (int i = 0; i < payAmt.length; i++) {
				firstAmt = payAmt[0].longValue();
				totalAmt = payAmt[1].longValue();
			}
			amountPayable[0] = firstAmt;
			amountPayable[1] = totalAmt;
		}
		return amountPayable;
	}

	@Override
	public boolean checkValidVinSts(List<PrjPrdDtlDTO> list, boolean nullAble) {
		if(CollectionUtils.isNotEmpty(list)){
			for (PrjPrdDtlDTO dto : list) {
				String vin = dto.getVin();
				String vinValidRes = dto.getVinValidRes();
				// 有VIN 并且验证不通过
				if(StringUtils.isNotBlank(vin)){
					if(!"1".equals(vinValidRes)){
						return false;
					}
				}
				// 没有VIN 并且不允许为空
				if(StringUtils.isBlank(vin) && !nullAble){
					return false;
				}
			}
			return true;
		}
		return false;
	}

    @Override
    public DataResultVO checkLnkIsChooseUsedCar(String lnkCompId, String prjTypCd) throws BaseException {
	    BaseLogUtils.info(logger, "checkLnkIsChooseUsedCar", "校验挂靠公司支持的车型入参为lnkCompId=" + lnkCompId + "prjTypCd=" + prjTypCd);
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        try {
            if (StringUtils.isEmpty(lnkCompId)) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("挂靠公司id为空");
                return result;
            }
            if (StringUtils.isEmpty(prjTypCd)) {
                result.setSuccess(BaseConstant.IS_YESNO_NO);
                result.setInfo("项目类型为空");
                return result;
            }
            List<KeyValueVO> keyValueVOList = splCommPropInfoApiService.checkLnkIsChooseUsedCar(Long.parseLong(lnkCompId),
                    CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), WebBaseConstant.CHANNEL_CD);
            BaseLogUtils.info(logger, "checkLnkIsChooseUsedCar", "查询挂靠公司支持的车型结果为keyValueVOList=" + JSON.toJSONString(keyValueVOList));
            if (CollectionUtils.isEmpty(keyValueVOList)) {
                //历史数据  不用校验
                result.setSuccess(BaseConstant.IS_YESNO_YES);
                return result;
            }
            Map<String, String> keyValueVOMap = new HashMap<>();
            for (KeyValueVO keyValueVO : keyValueVOList) {
                keyValueVOMap.put(keyValueVO.getKey(), keyValueVO.getValue());
            }
            //和对应的字典表PRJ0148里面的值相比较
            if (PrjBscInfoConstant.PRJ_TYP_CD_ESCJYD.equals(prjTypCd) || PrjBscInfoConstant.PRJ_TYP_CD_CDDYWUL.equals(prjTypCd) || PrjBscInfoConstant.PRJ_TYP_CD_OWNJYD.equals(prjTypCd)) {
                if (!keyValueVOMap.containsKey("002")) {
                    result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("该挂靠公司不支持二手车！");
                    return result;
                }
            } else {
                if (!keyValueVOMap.containsKey("001")) {
                    result.setSuccess(BaseConstant.IS_YESNO_NO);
                    result.setInfo("该挂靠公司不支持新车！");
                    return result;
                }
            }
            return result;
        } catch (BaseException e) {
            BaseLogUtils.error(logger, "checkLnkIsChooseUsedCar", "查询挂靠公司支持的车型发生异常e=" + e);
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(e.getMessage());
            return result;
        } catch (Exception e) {
            BaseLogUtils.error(logger, "checkLnkIsChooseUsedCar", "查询挂靠公司支持的车型发生异常e=" + e);
            result.setSuccess(BaseConstant.IS_YESNO_NO);
            result.setInfo(e.getMessage());
            return result;
        }
    }

	@Override
	public List<PrjPrdDtlVO> selectExpBusTwoList(PrjPrdDtlQueryCondition condition) throws BaseException {
		if (null == condition) {
			return null;
		}
		List<PrjPrdDtlVO> voList = PrjPrdDtlVOTransform.toVOList(this.projectApiService.selectExpBusTwoList(condition));
		List<Long> paySchIds = new ArrayList<>();
		if(CollectionUtils.isNotEmpty(voList)){
			for (PrjPrdDtlVO prjPrdDtlVO : voList){
				paySchIds.add(prjPrdDtlVO.getPaySchId());
			}
		}
		List<CapPayInfoDTO> capPayInfoDTOList = new ArrayList<>();
		List<Long> payIdList = new ArrayList<>();
		CapPayInfoQueryCondition capPayInfoQC = new CapPayInfoQueryCondition();
		if(CollectionUtils.isNotEmpty(paySchIds)){
			capPayInfoQC.setPaySchIdList(paySchIds);
			BaseLogUtils.newLogger("查询付款数据条件:" + JSON.toJSONString(capPayInfoQC)).info();
			capPayInfoDTOList = this.capPayInfoApiService.searchCapPayInfoList(capPayInfoQC);
			BaseLogUtils.newLogger("查询付款数据:" + JSON.toJSONString(capPayInfoDTOList)).setLogOperType(LogOperEnum.SELECT).setWarning(false).info();
		}
		if (CollectionUtils.isNotEmpty(capPayInfoDTOList)){
			for (CapPayInfoDTO capPayInfo : capPayInfoDTOList){
				payIdList.add(capPayInfo.getId());
			}
		}
		Map<Long, SysFlowSHDTO> flowSHDTOMap = null;
		if (CollectionUtils.isNotEmpty(payIdList)){
			//查询放款流程信息
			SysFlowSHQueryCondition queryCondition = new SysFlowSHQueryCondition();
			queryCondition.setSysCd(CurrentThreadContext.getCurrentSysCd());
			queryCondition.setDataIdList(payIdList);
			queryCondition.setWordsInTheTable(CapPayInfoConstant.CAP_FLOW_S_H_CAP);
			queryCondition.setStatus(1);
			BaseLogUtils.newLogger("查询放款流程记录条件:" + JSON.toJSONString(queryCondition)).info();
			List<SysFlowSHDTO> flowSHDTOList = flowQueryApiService.searchFlowSHList(queryCondition);
			BaseLogUtils.newLogger("查询放款流程记录:" + JSON.toJSONString(flowSHDTOList)).info();
			if (CollectionUtils.isNotEmpty(flowSHDTOList)) {
				flowSHDTOMap = new HashMap<>(flowSHDTOList.size());
				for (SysFlowSHDTO flow : flowSHDTOList) {
					//排除一条放款数据对应多个流程的情况 2017-02-21
					if (flowSHDTOMap.containsKey(flow.getDataId())) {
						continue;
					}
					flowSHDTOMap.put(flow.getDataId(), flow);
				}
			}
		}
		//放款状态
		Map<String, String> payStsCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), CapPayInfoConstant.CODE_PAY_STS_CD);
		if(CollectionUtils.isNotEmpty(voList)){
			if(CollectionUtils.isNotEmpty(capPayInfoDTOList)){
				for (PrjPrdDtlVO vo : voList){
					for (CapPayInfoDTO capPayInfoDTO : capPayInfoDTOList){
						if (vo.getPaySchId().equals(capPayInfoDTO.getPaySchId())){
							vo.setPayId(capPayInfoDTO.getId());
							vo.setBizFlag(capPayInfoDTO.getBizFlag());
							if (payStsCdMap != null && payStsCdMap.containsKey(capPayInfoDTO.getPayStsCd())) {
								vo.setPayStsCd(payStsCdMap.get(capPayInfoDTO.getPayStsCd()));
							}
							if (null != flowSHDTOMap && flowSHDTOMap.containsKey(capPayInfoDTO.getId())) {
								SysFlowSHDTO sysFlowSHDTO = flowSHDTOMap.get(capPayInfoDTO.getId());
								vo.setPrInsNo(sysFlowSHDTO.getPNo());
								vo.setPrInsId(sysFlowSHDTO.getPInstId());
								vo.setAudStsCd(sysFlowSHDTO.getAudStsCd());
							}
						}
					}
				}
			}
		}
		//风险隐患
		Map<String, String> hidRiskCdMap = this.sysDictionaryApiService.searchGroupToMap(CurrentThreadContext.getCurrentSysCd(), CurrentThreadContext.getCurrentOrgCd(), DictionaryConstant.CODE_HID_RISK_CD);
		if(CollectionUtils.isNotEmpty(voList)){
			for (PrjPrdDtlVO vo : voList){
				if(hidRiskCdMap.containsKey(vo.getHidRisk())){
					vo.setHidRisk(hidRiskCdMap.get(vo.getHidRisk()));//风险隐患
				}
			}
		}
		BaseLogUtils.newLogger("查询二级外派流程业务数据voList:" + JSON.toJSONString(voList)).info();
		return voList;
	}

	/**
	 * 商家 轻卡 修改挂靠公司
	 * @param entity
	 * @return
	 * @throws BaseException
	 */
	@Override
	public boolean modifyLnkByLcv(PrjPrdDtlVO entity) throws BaseException {
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return projectModifyApiService.modifyLnkByLcv(PrjPrdDtlVOTransform.toDTO(entity));
	}

}
